package com.pinda.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.pinda.base.entity.PdTruckLicense;
import com.pinda.commonutils.Result;
import com.pinda.commonutils.StatusCode;
import com.pinda.service.IPdTruckLicenseService;
import com.pinda.service.IPdTruckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

/**
 * <p>
 * 车辆行驶证表 前端控制器
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd-truck-license")
public class PdTruckLicenseController {


    @Autowired
    IPdTruckLicenseService iPdTruckLicenseService;;


    /**
     * 修改车俩行驶征信息
     * @param pdTruckLicense
     * @return
     */
    @PostMapping("/updateTruckLicense")
    public Result updateTruckLicense(@RequestBody PdTruckLicense pdTruckLicense){
        boolean b = this.iPdTruckLicenseService.updateById(pdTruckLicense);
        if(b){
            return new Result<>(true, StatusCode.OK,"ok",b);
        }
        return new Result<>(false, StatusCode.ERROR,"no",b);
    }


    /**
     * 添加车俩行驶征的车俩图片信息
     * @param myfile
     * @return
     */
    @PostMapping("uploadcontrollerPdTruckLicense")
    public String uploadcontrollerPdTruckLicense(@RequestBody() MultipartFile myfile) {
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "oss-cn-hangzhou.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId = "LTAI5tBDtxb5zU2extkovwUW";
        String accessKeySecret = "oFZi4U5KRMtq1kZy8igFLCXcOD9l2U";
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "projectvue";
        //你上传到oss后的名字 会根据日期帮你创建文件夹。
        String objectName = String.valueOf(UUID.randomUUID());
        String hz = myfile.getOriginalFilename().substring(myfile.getOriginalFilename().indexOf(".") + 1);
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            InputStream inputStream = myfile.getInputStream();
            // 创建PutObject请求。
            ossClient.putObject(bucketName, objectName + "." + hz, inputStream);
        } catch (Exception oe) {

        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        //https://qy151.oss-cn-hangzhou.aliyuncs.com/2022/6/10/20d3d7e6b5bb455cb548675501f7270fgdnj.jpg
        String url = "https://" + bucketName + "." + endpoint + "/" + objectName + "." + hz;
        return url;
    }




    @PostMapping("/queryById/{id}")
    public Result<PdTruckLicense> queryPdTruckLicenseById(@PathVariable("id")Integer id){
        PdTruckLicense byId = this.iPdTruckLicenseService.getById(id);

        if(byId!=null){
            return new Result<>(true, StatusCode.OK,"ok",byId);
        }
        return new Result<>(false, StatusCode.ERROR,"no",byId);
    }






}
